import os, requests, re
from requests.exceptions import *

header = {'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
path = os.getcwd()


def getHtml(url):
    """
    get a code from a html through url.
    :param url:
    :return: A str about the html.
    """
    global page
    try:
        page = requests.get(url, headers=header, timeout=3)
        page.encoding = "utf-8"
        if page.status_code is 200:
            page = page.text
        else:
            print("网页获取获取错误。")
            return 'Error'
    except ConnectionError:
        print("连接错误")
        return 'Error'
    except TimeoutError:
        print("连接超时")
        return 'Error'
    except ReadTimeout:
        print("连接超时")
        return 'Error'
    if not os.path.exists('./Support'):
        os.mkdir('./Support')
    with open("./Support/tempHTML.html", mode='w', encoding='utf8')as f:
        f.write(f"{page}")
    return page


def photoURL(text):
    """
    The function is that get URL list from a HTML.

    :param text:
    :return: listURL, HTML name
    """
    urlHard = 'https://eternallybored.org/misc/wget/src/'
    urlLast = re.findall(r'<a href="(.*?)">.*?</a>', text)
    listURL = []
    for url in urlLast:
        urlDir = urlHard + url
        listURL.append(urlDir)

    if len(listURL) is 0:
        print("The HTML has not URL for photo.")
        return 'Error'
    name = 'wget'
    with open('./Support/photoURL.txt', mode='w', encoding='utf8') as f:
        f.write(f'{name}\n')
        for i in listURL:
            f.write(f'{i}\n')
    return listURL, name


def getAPhoto(photo_url, gallery):
    """
    Download1024 photo for the gallery.
    :param photo_url: A url in internet for a photo.
    :param gallery: The photo belong to the gallery.
    :return:successful or error.
    """
    if os.path.exists(f'{gallery}/{photo_url[-12:]}'):
        return 'exist'
    try:
        photo = requests.get(photo_url, headers=header, timeout=3)  # 获取图片
    except TimeoutError:
        return 'Timeout'
    # 写入文件
    try:
        name = re.findall(r"https://eternallybored.org/misc/wget/src/(.*)", photo_url)[0]
        with open(f'{gallery}/{name}', 'wb') as p:
            p.write(photo.content)
    except FileNotFoundError:
        pass
    except OSError:
        pass
    if os.path.exists(f'{gallery}/{name}'):
        return 'successful'


def writeAll(urllist, name):
    """
    Create a folder for this gallery and download those images.
    :param urllist: some url of photos in the list.
    :param name: The photo gallery's name.
    :return: Successful or Error.
    """
    if not os.path.exists('./photos'):
        os.mkdir('./photos')
    if not os.path.exists(f'./photos/{name}'):
        os.mkdir(f'./photos/{name}')
    # 缩进问题一定要注意啊
    name = './photos/' + name
    print('path', end=':')
    print(f'{path}{name[1:]}')
    for url in urllist:
        c = getAPhoto(url, name)
        if c is 'successful':
            #pass
            print(f'{url[-12:]}写入成功', end='\b'*16)
            # print('\b'*)
        elif c is 'exist':
            print(f"The file ({url[-12:]}) is already Exist.")
        else:
            print(f'{url[-12:]}写入失败')
    return 'successful'


def main(url):
    """ The function can finish the script all of things.
        Convenient for other script calls.
        """
    #
    html = getHtml(url)
    if html is 'Error':
        print('GetHtmlError:None')
        return 'Error'
    #
    listPhotoURL = photoURL(html)
    if listPhotoURL is 'Error':
        print('ListPhoto Error: The HTML in URL has not photos.')
        return 'Error'
    #
    writeAll(listPhotoURL[0], listPhotoURL[1])  # list, name
    #
    for f in os.walk(f'{path}/photos/{listPhotoURL[1]}'):
        pass
    print(f'成功下载{len(f[2])}个，下载失败{len(listPhotoURL[0])-len(f[2])}')
    return 'successful'


if __name__ == '__main__':
    from HTTP.downloadWget.getDirectory import main as m
    URL = r'https://eternallybored.org/misc/wget/src/'
    main(URL)
